package com.bw.day03.userinfo

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.Res
import com.bw.base.net.entity.UploadEntity
import com.bw.base.net.entity.UserEntity
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch
import okhttp3.MultipartBody

class UserViewModel:BaseViewModel() {
    val userSuccess = MutableLiveData<Res<UserEntity>>()
    val userFail=MutableLiveData<String>()
    fun user(){
        viewModelScope.launch {
            try {
                userSuccess.value=apiService.user()
            }catch (e:Exception){
                userFail.value=e.message
            }
        }
    }
    val uploadSuccess = SingleLiveData<Res<UploadEntity>>()
    val uploadFail  = SingleLiveData<String>()
    fun upload(model:String,part: MultipartBody.Part,pid:String){
        viewModelScope.launch {
            try {
                uploadSuccess.postValue(apiService.upload(model,part,pid))
            }catch (e:Exception){
                uploadFail.postValue(e.message.toString())
            }
        }
    }
}